﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;
using System.IO;

namespace Web.Account
{
    public partial class EditRole : System.Web.UI.Page
    {
        static string prevPage = String.Empty;
        static ROLE role;
        static long roleID;
        static USER user;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if (((bool)user.IS_ADMIN) || ((bool)r.IS_MANAGER))
                {
                    string requestEdit = Request.QueryString["id"];
                    if (requestEdit != null)
                    {
                        roleID = Convert.ToInt64(requestEdit);
                        LoadDataRoleEdit();
                    }
                }
                else
                {
                    Response.Redirect("../Home.aspx");
                }
            }
        }
        private void LoadDataRoleEdit()
        {
            role = RoleService.GetRoleByID(roleID);
            if (role != null)
            {
                txtRoleName.Text = role.ROLE_NAME;
                txtDescription.Text = role.DESCRIPTION;
                btnToggleIsManger.Checked = (bool)role.IS_MANAGER;
                btnToggleActive.Checked = role.ACTIVE;
            }
            else
            {
                divContent.Style["display"] = "none";
                divError.Style["display"] = "block";
            }
        }
        private int updateRole()
        {
            if (btnToggleIsManger.Checked == true)
                role.IS_MANAGER = true;
            else
                role.IS_MANAGER = false;
            if (btnToggleActive.Checked == true)
                role.ACTIVE = true;
            else
                role.ACTIVE = false;
            role.DESCRIPTION = txtDescription.Text.Trim();
            role.ROLE_NAME = txtRoleName.Text.Trim();

            return RoleService.UpdateRole(role);
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int updateR = updateRole();
            if (updateR == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Update Role successful !" + "','" + "Roles.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Update Role successful !", "Roles.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not update this Role !" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Can not update Role !", "");

        }
        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(prevPage);
        }
        protected void btn_Delete_ServerClick(object sender, EventArgs e)
        {
            ROLE rol = RoleService.GetRoleByID(roleID);
            int roleDel = RoleService.DeleteRole(roleID);
            if (roleDel == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Delete role successful !" + "','" + "Roles.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Delete ROLE successful !", "Roles.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not delete this role !" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Can not delete ROLE !", "");

        }
    }
}